<?php

namespace Admin\Controller;
/**
 * 财务管理
 * @author Dinghua
 *
 */
class FinanceController extends AdminController{
	
	/**
	 * 业绩
	 */
	public function index() {
		$map = array();
		
		$title = I('get.title');
		if ($title) {
			$map['username'] = array('like',"%$title%");
		}
		
		$map['ordstatus'] = 4;
		
		if ( isset($_GET['time-start']) ) {
			$map['ordtime'][] = array('egt',strtotime(I('time-start')));
		}
		if ( isset($_GET['time-end']) ) {
			$map['ordtime'][] = array('elt',24*60*60 + strtotime(I('time-end')));
		}
		
		$list = $this->lists('Order', $map, 'id DESC');

		foreach ($list as $key => $val) {
			
			$lists[$val['userid']]['info'] = $val;
			$lists[$val['userid']]['ordfee'] += $val['ordfee'];
		}
		
		
		$this->assign('timestart', I('time-start'));
		$this->assign('timeend', I('time-end'));
		$this->assign('lists', $lists);
		$this->meta_title = '业务员业绩';
		$this->display();
	}
	
	
	public function view($uid = NULL, $timestart = NULL, $timeend = NULL) {
		
		if (empty($uid))  $this->error('参数错误！');
		$Order = M('Order');
		$map = array();

		$this->assign('uid', $uid);
		$nickname = D('Member')->where(array('uid' => $uid))->getField('nickname');
		$this->assign('nickname', $nickname);

		// 总金额
		$map1['ordstatus'] = 4;
		$map1['userid'] = $uid;
		$where1['_complex'] = $map1;
		if ( $timestart ) {
			$where1['ordtime'][] = array('egt',strtotime($timestart));
		}
		if ( $timeend ) {
			$where1['ordtime'][] = array('elt',24*60*60 + strtotime($timeend));
		}
		if ( isset($_GET['time-start']) ) {
			$where1['ordtime'][] = array('egt',strtotime(I('time-start')));
		}
		if ( isset($_GET['time-end']) ) {
			$where1['ordtime'][] = array('elt',24*60*60 + strtotime(I('time-end')));
		}
		$ordfee = $Order->where($where1)->sum('ordfee');

		// 退额
		$map2['ordstatus'] = 5;
		$map2['userid'] = $uid;
		$where2['_complex'] = $map2;
		if ( $timestart ) {
			$where2['ordtime'][] = array('egt',strtotime($timestart));
			$ts = $timestart;
		}
		if ( $timeend ) {
			$where2['ordtime'][] = array('elt',24*60*60 + strtotime($timeend));
			$te = $timeend;
		}
		if ( isset($_GET['time-start']) ) {
			$where2['ordtime'][] = array('egt',strtotime(I('time-start')));
			$ts = I('time-start');
		}
		if ( isset($_GET['time-end']) ) {
			$where2['ordtime'][] = array('elt',24*60*60 + strtotime(I('time-end')));
			$te = I('time-end');
		}
		$outfee = $Order->where($where2)->sum('ordfee');
	
		$this->assign('ts', $ts);
		$this->assign('te', $te);
		// 分页
		$map3['ordstatus'] = array('in', '4,5');
		$map3['userid'] = $uid;
		$where3['_complex'] = $map3;
		if ( $timestart ) {
			$where3['ordtime'][] = array('egt',strtotime($timestart));
		}
		if ( $timeend ) {
			$where3['ordtime'][] = array('elt',24*60*60 + strtotime($timeend));
		}

		if ( isset($_GET['time-start']) ) {
			$where3['ordtime'][] = array('egt',strtotime(I('time-start')));
		}
		if ( isset($_GET['time-end']) ) {
			$where3['ordtime'][] = array('elt',24*60*60 + strtotime(I('time-end')));
		}

		$list = $this->lists('Order', $where3, 'id DESC');

		foreach ($list as $key => $val) {
			if ($val['ordstatus'] == 4) {
				$money['ordfee'] += $val['ordfee'];
			}
			if ($val['ordstatus'] == 5) {
				$money['outfee'] += $val['ordfee'];
			}
		}
		
		$this->assign('ordfee', $ordfee);
		$this->assign('outfee', $outfee);
		$this->assign('money', $money);
		$this->assign('_list', $list);
		
		$this->meta_title = '业务员业绩';
		$this->display();
	}

	
}